﻿<div class="container bs-docs-container">
    <div class="row">
        <div class="col-md-3">
            <div class="bs-sidebar hidden-print" role="complementary">
                <ul id="treeDetailType" class="nav bs-sidenav mini-tree"
                    showTreeIcon="true" textField="Name" idField="id">
                </ul>
            </div>
        </div>
        <div class="col-md-9" role="main">
            <div class="bs-docs-section">
                <div id="datagrid" class="mini-datagrid"
                    idField="id" 
                    onselectionchanged="onSelectionChanged">
                    <div property="columns">
                        <div field="Title" width="120" headerAlign="center" allowSort="false">标题</div>        
                        <div field="Remarks" width="100" allowSort="false">描述</div>
                        <div field="Cases" width="100" allowSort="false">案例</div>
                        <div field="DisposeType_Name" width="100" allowSort="false">处理类型</div>
                    </div>
                </div>  
            </div>
        </div>
    </div>
</div>

@section Styles{
    <link rel="stylesheet" type="text/css" href="/Content/knowledge.assets/miniui/themes/default/miniui.css" />
    <link rel="stylesheet" type="text/css" href="/Content/knowledge.assets/miniui/themes/icons.css" />
    <style type="text/css">
        .bs-docs-section {
            margin-top: 30px;
        }
    </style>
}

@section Scripts{
    <script type="text/javascript" src="/Content/knowledge.assets/miniui/miniui.js"></script>
    <script type="text/javascript">
        mini.parse();

        $(function () {
            //初始化树
            $.ajax({
                url: '/Model/UserKnowledgeDetailsType/?userID=' + userJson.id,
                type: 'GET'
            }).then(function (data) {
                var arrResult = [];
                $.each(data, function (i, json) {
                    if (!json.ParentCode) {
                        arrResult.push(json);
                    };
                });
                var _findChildren = function (parentJson) {
                    $.each(data, function (i, json) {
                        if (json.ParentCode == (parentJson.ParentCode ? parentJson.ParentCode + '.' : '') + parentJson.Code) {
                            parentJson.children = parentJson.children || [];
                            parentJson.children.push(json);
                        };
                    });
                    if (parentJson.children) {
                        parentJson.children.forEach(function (result) {
                            _findChildren(result)
                        });
                    }
                };
                arrResult.forEach(function (result) {
                    _findChildren(result)
                });

                console.info(arrResult)

                var treeDetailType = mini.get("treeDetailType");
                treeDetailType.loadData(arrResult);
                $('#treeDetailType').prepend([
                    '<li class="clearfix">',
                    '<a href="#" class="pull-right" onclick="delDetailType();">删除</a>',
                    '<a href="#" class="pull-right" onclick="editDetailType();">编辑</a>',
                    '<a href="#" class="pull-right" onclick="addDetailType();">新增</a>',
                    '</li>'
                ].join(''));

                //初始化列表
                var grid = mini.get("datagrid");
                treeDetailType.on("nodeselect", function (e) {
                    if (e.isLeaf) {
                        $.ajax({
                            url: '/Model/KnowledgeDetail/CURD?userDetailType=' + e.node.id,
                            type: 'GET'
                        }).then(function (data) {
                            var json = $.parseJSON(data);
                            grid.setData(json);
                            grid.setTotalCount(json.length);
                        });
                    } else {
                        grid.setData([]);
                        grid.setTotalCount(0);
                    }
                });
                grid.on('selectionchanged', function () {
                });
            });
        });

        function addDetailType() {
            var tree = mini.get("treeDetailType");
            var node = tree.getSelectedNode();

            mini.open({
                url: '/Content/knowledge.assets/html/UserKnowledgeDetailType.Edit.html',
                title: "用户知识细类明细", width: 400, height: 160,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { action: "new", User_ID: userJson.id };
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    location.href = location.href;
                }
            });
        }

        function editDetailType() {
            var tree = mini.get("treeDetailType");
            var node = tree.getSelectedNode();

            if (!node) {
                mini.alert('未选中记录！');
                return;
            }

            mini.open({
                url: '/Content/knowledge.assets/html/UserKnowledgeDetailType.Edit.html',
                title: "用户知识细类明细", width: 600, height: 360,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { action: "edit", id: node.id };
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    location.href = location.href;
                }
            });
        }

        function delDetailType() {
            var tree = mini.get("treeDetailType");
            var node = tree.getSelectedNode();

            if (!node) {
                mini.alert('未选中记录！');
                return;
            }
            mini.confirm("确定删除选中节点?", "确认信息",
                function (action) {
                    if (action == "ok") {
                        $.ajax({
                            url: "/Model/UserKnowledgeDetailsType/CURD/" + node.id,
                            type: 'DELETE'
                        }).then(function (ar) {
                            if (ar.Success) {
                                tree.removeNode(node);
                            }
                        });
                    }
                });
        }

    </script>
}